package com.game.boxman;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class BoxManPanel extends JPanel {
    private static final int CELL_SIZE = 50; // 每个单元格的大小
    private static final Color WALL_COLOR = Color.BLACK; // 墙的颜色
    private static final Color FLOOR_COLOR = Color.WHITE; // 地板的颜色
    private static final Color PLAYER_COLOR = Color.BLUE; // 玩家的颜色
    private static final Color BOX_COLOR = Color.RED; // 箱子的颜色
    private static final Color TARGET_COLOR = Color.GREEN; // 目标位置的颜色

    private final GameMap gameMap;
    private int playerX, playerY;

    public BoxManPanel() {
        this.gameMap = new GameMap();
        this.playerX = gameMap.getPlayerX();
        this.playerY = gameMap.getPlayerY();

        setPreferredSize(new Dimension(gameMap.getCols() * CELL_SIZE, gameMap.getRows() * CELL_SIZE));
        setBackground(Color.LIGHT_GRAY);
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                movePlayer(e.getKeyCode());
            }
        });
    }

    private void movePlayer(int keyCode) {
        int newX = playerX;
        int newY = playerY;

        switch (keyCode) {
            case KeyEvent.VK_UP:
                newY--;
                break;
            case KeyEvent.VK_DOWN:
                newY++;
                break;
            case KeyEvent.VK_LEFT:
                newX--;
                break;
            case KeyEvent.VK_RIGHT:
                newX++;
                break;
        }

        if (gameMap.isMoveValid(playerX, playerY, newX, newY)) {
            if (gameMap.isBox(newX, newY)) {
                int boxNewX = newX + (newX - playerX);
                int boxNewY = newY + (newY - playerY);

                if (gameMap.isFloor(boxNewX, boxNewY)) {
                    gameMap.moveBox(newX, newY, boxNewX, boxNewY);
                    playerX = newX;
                    playerY = newY;
                }
            } else {
                playerX = newX;
                playerY = newY;
            }

            if (gameMap.isAllBoxesOnTargets()) {
                JOptionPane.showMessageDialog(this, "恭喜过关！");
            }

            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawBoard(g);
    }

    private void drawBoard(Graphics g) {
        for (int y = 0; y < gameMap.getRows(); y++) {
            for (int x = 0; x < gameMap.getCols(); x++) {
                int x1 = x * CELL_SIZE;
                int y1 = y * CELL_SIZE;
                int x2 = x1 + CELL_SIZE;
                int y2 = y1 + CELL_SIZE;

                if (gameMap.isWall(x, y)) {
                    g.setColor(WALL_COLOR);
                    g.fillRect(x1, y1, CELL_SIZE, CELL_SIZE);
                } else if (gameMap.isFloor(x, y)) {
                    g.setColor(FLOOR_COLOR);
                    g.fillRect(x1, y1, CELL_SIZE, CELL_SIZE);
                } else if (gameMap.isTarget(x, y)) {
                    g.setColor(TARGET_COLOR);
                    g.fillRect(x1, y1, CELL_SIZE, CELL_SIZE);
                }

                if (x == playerX && y == playerY) {
                    g.setColor(PLAYER_COLOR);
                    g.fillOval(x1 + 5, y1 + 5, CELL_SIZE - 10, CELL_SIZE - 10);
                }

                if (gameMap.isBox(x, y)) {
                    g.setColor(BOX_COLOR);
                    g.fillRect(x1 + 5, y1 + 5, CELL_SIZE - 10, CELL_SIZE - 10);
                }
            }
        }
    }
}